package com.hzqy.service.epg;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.hzqy.persistence.dao.epg.HappyDao;
import com.hzqy.web.vo.CmContentVo;
import com.hzqy.web.vo.activity.ActivityVoteUserVo;

@Service
public class HappyServiceBean {
	protected final Logger LOG = LoggerFactory.getLogger(getClass());
	@Resource
	private HappyDao happyDao;

	/**
	 * 查询参赛人员名单
	 * @param ccv
	 * @param page
	 * @param rows
	 * @return
	 */
	public Map<String, Object> selectHappyList(CmContentVo ccv,String f_avu_activityid, int page, int rows) {
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("f_cc_director", ccv.getF_cc_director());
		map.put("f_cc_seriescount", ccv.getF_cc_seriescount());
		map.put("f_cc_code", ccv.getF_cc_code());
		map.put("f_cc_type", ccv.getF_cc_type());
		map.put("f_avu_activityid", f_avu_activityid);
		int rowCount = happyDao.selectHappyCount(map);
		map.put("total", rowCount);
		if(rowCount > 0 && page > 0 && rows > 0) {
			map.put("firstResult", (page-1)*rows);
			map.put("maxResults", rows);
			List<CmContentVo> results =happyDao.selectHappyList(map);
			for(int i=0;i<results.size();i++) {
				results.get(i).setF_cc_desc(results.get(i).getF_cc_desc().replaceAll("<p>", "").replaceAll("</p>", ""));
				String str = HappyServiceBean.longOfString(results.get(i).getF_cc_onlinedate());
				results.get(i).setF_cc_onlinedate(str);//结束时间  20190108095202
			}
			map.put("rows", results);
		} else {
			map.put("rows", new ArrayList<CmContentVo>());
		}
		return map;
	}

	/**
	 * 插入投票记录
	 * @param acvote
	 * @return
	 */
	public int insertVoteUser(ActivityVoteUserVo acvote) {
		return happyDao.insertVoteUser(acvote);
	}
	/**
	 * 查询票数
	 * @param avu
	 * @return
	 */
	public int selectSumVotenum (ActivityVoteUserVo avu ) {
		return happyDao.selectSumVotenum(avu);
	}
	
	public int  selectContentId(String f_cc_code) {
		Map<String,Object> map = new HashMap<String, Object>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time=sdf.format(new Date());
		map.put("f_cc_code", f_cc_code);
		map.put("f_cc_onlinedate", time);
		return happyDao.selectContentId(map);
	}
	
	/**
	 * @param first
	 * @param second
	 * @return
	 * @throws ParseException 
	 * @throws java.text.ParseException 
	 */
	private  static String  longOfString(String time){
		time.replace("-","");
		String result = time.replace("-", "").replace(":", "").replace(" ","").replace(".0", "").trim();
		return result;
	}

}
